package com.jspx.txweb.action;

import com.jspx.boot.sign.LanguageRes;
import org.slf4j.Logger;
import com.jspx.boot.environment.Environment;
import org.slf4j.LoggerFactory;
import com.jspx.io.AbstractRead;
import com.jspx.io.AutoReadTextFile;
import com.jspx.json.JSONObject;
import com.jspx.sober.Transaction;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Operate;
import com.jspx.txweb.dao.impl.DFAFilterImpl;
import com.jspx.txweb.table.BlockedWord;
import com.jspx.txweb.view.WordFilterView;
import com.jspx.utils.FileUtil;
import com.jspx.utils.StringUtil;

/**
 * Created by yuan on 2015/6/16 0016.
 *
 */
@HttpMethod(caption = "屏蔽关键词")
public class WordFilterAction extends WordFilterView {
    private static final Logger log = LoggerFactory.getLogger(WordFilterAction.class);
    private JSONObject json = new JSONObject();
    private String encode = Environment.defaultEncode;

    public WordFilterAction()
    {
        json.put(SUCCESS, 0);
        json.put(ERROR, 1);
        json.put(Environment.infoType, Environment.warningInfo);
    }

    public void setEncode(String encode) {
        this.encode = encode;
    }

    private String[] fileName;
    public void setFileName(String[] fileName) {
        this.fileName = fileName;
    }


    //这里将会清空所有数据
    @Operate(caption="导入文件")
    public void importFile() throws Exception
    {

        DFAFilterImpl dfaFilter = (DFAFilterImpl)filter;
        int row = 0;
        boolean result;
        Transaction transaction = dfaFilter.createTransaction();
        try
        {
            if (dfaFilter.deleteAll())
            for (String f:fileName)
            {
                if (!FileUtil.isFileExist(f)) continue;
                AbstractRead abstractRead = new AutoReadTextFile();
                abstractRead.setFile(f);
                abstractRead.setEncode(encode);
                String txt = abstractRead.getContent();
                if (StringUtil.isNULL(txt)) continue;
                row = row+ dfaFilter.importWord(txt);
            }
            result = true;
            transaction.commit();
        } catch (Exception e) {
            result = false;
            transaction.rollback();
            log.error("导入屏蔽关键字错误",e);
        }
        json.put(Environment.infoType,Environment.promptInfo);
        if (result)
        {
            json.put(Environment.id_success, 1);
            json.put(Environment.rowCount, row);
            json.put(ERROR, 0);
            json.put(Environment.id_message,language.getLang(LanguageRes.operationSuccess));
        } else
        {
            json.put(Environment.id_success, 0);
            json.put(Environment.rowCount, row);
            json.put(ERROR, 1);
            json.put(Environment.id_message,language.getLang(LanguageRes.operationFailure));
        }
    }

    //不清空数据，累加方式
    @Operate(caption="导入提交")
    public void importWord() throws Exception
    {
        String content = getString("content");
        if (content==null)
        {
            json.put(Environment.id_success, 0);
            json.put(Environment.rowCount, 0);
            json.put(ERROR, 1);
            json.put(Environment.id_message,language.getLang(LanguageRes.operationFailure));
            addFieldInfo(Environment.warningInfo,language.getLang(LanguageRes.invalidParameter));
            return;
        }
        DFAFilterImpl dfaFilter = (DFAFilterImpl)filter;
        int row  = dfaFilter.checkImportWord(content);
        if (row>=0)
        {
            json.put(Environment.id_success, 1);
            json.put(Environment.rowCount, row);
            json.put(ERROR, 0);
            json.put(Environment.id_message,language.getLang(LanguageRes.operationSuccess));
        } else
        {
            json.put(Environment.id_success, 0);
            json.put(Environment.rowCount, row);
            json.put(ERROR, 1);
            json.put(Environment.id_message,language.getLang(LanguageRes.operationFailure));
        }
    }

    //不清空数据，累加方式
    @Operate(caption="清空")
    public void delete() throws Exception
    {
        DFAFilterImpl dfaFilter = (DFAFilterImpl)filter;
        if (dfaFilter.deleteAll())
        {
            json.put(Environment.id_success, 1);
            json.put(ERROR, 0);
            json.put(Environment.id_message,language.getLang(LanguageRes.operationSuccess));
        } else
        {
            json.put(Environment.id_success, 0);
            json.put(ERROR, 1);
            json.put(Environment.id_message,language.getLang(LanguageRes.operationFailure));
        }
        setResult(json);
    }

    @Override
    public String execute() throws Exception {
        if (isMethodInvoked()) {
            DFAFilterImpl dfaFilter = (DFAFilterImpl)filter;
            dfaFilter.evict(BlockedWord.class);
            setResult(json);
        }
        return super.execute();
    }
}
